/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfy.idcp.modules.crawl.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfy.idcp.modules.crawl.entity.CrawlWebMain;
import com.lczyfy.idcp.modules.crawl.service.CrawlRelationService;
import com.lczyfy.idcp.modules.crawl.service.CrawlWebMainService;
import com.lczyfy.idcp.modules.crawl.service.CrawlRelationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfy.idcp.common.config.Global;
import com.lczyfy.idcp.common.persistence.Page;
import com.lczyfy.idcp.common.web.BaseController;
import com.lczyfy.idcp.common.utils.StringUtils;
import com.lczyfy.idcp.modules.crawl.entity.CrawlWebMain;
import com.lczyfy.idcp.modules.crawl.service.CrawlWebMainService;

/**
 * 爬虫目标Controller
 * @author cw
 * @version 2018-07-20
 */
@Controller
@RequestMapping(value = "${adminPath}/crawl/crawlWebMain")
public class CrawlWebMainController extends BaseController {

	@Autowired
	private CrawlWebMainService crawlWebMainService;
    @Autowired
    private CrawlRelationService crawlRelationService;
	@ModelAttribute
	public CrawlWebMain get(@RequestParam(required=false) String id) {
		CrawlWebMain entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = crawlWebMainService.get(id);
		}
		if (entity == null){
			entity = new CrawlWebMain();
		}
		return entity;
	}
	
	@RequiresPermissions("crawl:crawlWebMain:view")
	@RequestMapping(value = {"list", ""})
	public String list(CrawlWebMain crawlWebMain, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CrawlWebMain> page = crawlWebMainService.findPage(new Page<CrawlWebMain>(request, response), crawlWebMain); 
		model.addAttribute("page", page);
		return "modules/crawl/crawlWebMainList";
	}

	@RequiresPermissions("crawl:crawlWebMain:view")
	@RequestMapping(value = "form")
	public String form(CrawlWebMain crawlWebMain, Model model) {
		model.addAttribute("crawlWebMain", crawlWebMain);
		return "modules/crawl/crawlWebMainForm";
	}

	@RequiresPermissions("crawl:crawlWebMain:edit")
	@RequestMapping(value = "save")
	public String save(CrawlWebMain crawlWebMain, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, crawlWebMain)){
			return form(crawlWebMain, model);
		}

		crawlWebMainService.save(crawlWebMain);
		addMessage(redirectAttributes, "保存爬虫目标成功");
		return "redirect:"+Global.getAdminPath()+"/crawl/crawlWebMain/?repage";
	}
	
	@RequiresPermissions("crawl:crawlWebMain:edit")
	@RequestMapping(value = "delete")
	public String delete(CrawlWebMain crawlWebMain, RedirectAttributes redirectAttributes) {
		crawlWebMainService.delete(crawlWebMain);
		addMessage(redirectAttributes, "删除爬虫目标成功");
		return "redirect:"+Global.getAdminPath()+"/crawl/crawlWebMain/?repage";
	}

}